package cn.tedu.tmall.admin.mall.dao.repository.impl;

import cn.tedu.tmall.admin.mall.dao.mapper.MallGoodsDetailMapper;
import cn.tedu.tmall.admin.mall.dao.mapper.MallGoodsMapper;
import cn.tedu.tmall.admin.mall.dao.repository.IMallGoodsRepository;
import cn.tedu.tmall.admin.mall.pojo.po.MallGoodsDetailPO;
import cn.tedu.tmall.admin.mall.pojo.po.MallGoodsPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 数据操作层-商城商品接口实现类
 */
@Repository
public class MallGoodsRepository implements IMallGoodsRepository {
    @Autowired
    private MallGoodsMapper mallGoodsMapper;
    @Autowired
    private MallGoodsDetailMapper mallGoodsDetailMapper;

    @Override
    public int countGoodsByTitle(String title) {
        QueryWrapper<MallGoodsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title",title);
        return mallGoodsMapper.selectCount(queryWrapper);
    }

    @Override
    public int saveGoods(MallGoodsPO mallGoodsPO) {
        return mallGoodsMapper.insert(mallGoodsPO);
    }

    @Override
    public int saveGoodsDetail(MallGoodsDetailPO mallGoodsDetailPO) {
        return mallGoodsDetailMapper.insert(mallGoodsDetailPO);
    }

    @Override
    public List<MallGoodsPO> listGoods(Long categoryId) {
        QueryWrapper<MallGoodsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        return mallGoodsMapper.selectList(queryWrapper);
    }

    @Override
    public int countGoodsById(Long goodsId) {
        QueryWrapper<MallGoodsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",goodsId);
        return mallGoodsMapper.selectCount(queryWrapper);
    }

    @Override
    public MallGoodsPO getGoodsBasicById(Long goodsId) {
        return mallGoodsMapper.selectById(goodsId);
    }

    @Override
    public MallGoodsDetailPO getGoodsDetailByGoodsId(Long goodsId) {
        QueryWrapper<MallGoodsDetailPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId);
        return mallGoodsDetailMapper.selectOne(queryWrapper);
    }
}
